package com.csdj.youxi.controller;

import com.alibaba.fastjson.JSON;
import com.csdj.youxi.entity.AppInfo;
import com.csdj.youxi.service.IAppInfoService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AppInfoController {

    @Resource
    private IAppInfoService iAppInfoService;

    @RequestMapping("/toaddAi")
    public String toaddAi(){
        return "developer/appinfoadd";
    }
    @RequestMapping("/toselAp")
    public String toselAp(){
        return "developer/appinfolist";
    }
    @RequestMapping("/toindexAi")
    public String toindexAi(){
        return "developer/index";
    }
    @RequestMapping("/toupdate")
    public String toUpdate(){
        return "developer/appinfoxiugai";
    }
    @RequestMapping("/toshenheAp")
    public String toshenheAp(){ return "backend/applist"; }

    @ResponseBody
    @PostMapping("indexAi")
    public String indexAi(@RequestParam(value = "id",required = false) int id){
        AppInfo appInfo = iAppInfoService.selAiCha(id);
        return JSON.toJSONString(appInfo);
    }

    @ResponseBody
    @GetMapping("/selAi")
    public String selAp(){
         List<AppInfo> list = iAppInfoService.selAi();
         return JSON.toJSONString(list);
    }

    @ResponseBody
    @PostMapping("/deleteAi")
    public String delteAp(@RequestParam(value = "id" , required = false) int id){
        int num = iAppInfoService.deleteAi(id);
        if(num>0){
            return "true";
        }else {
            return "false";
        }
    }

    @ResponseBody
    @GetMapping("/insertAi")
    public String insertAi(
                           @RequestParam(value = "softwareName" , required = false) String softwareName,
                           @RequestParam(value = "apkName" , required = false) String apkName,
                           @RequestParam(value = "supportRom" , required = false) String supportRom,
                           @RequestParam(value = "interfaceLanguage" , required = false) String interfaceLanguage,
                           @RequestParam(value = "flatformId" , required = false) int flatformId,
                           @RequestParam(value = "categoryLeve11" , required = false) int categoryLeve11,
                           @RequestParam(value = "categoryLeve12" , required = false) int categoryLeve12,
                           @RequestParam(value = "categoryLeve13" , required = false) int categoryLeve13,
                           @RequestParam(value = "softwareSize" , required = false) int softwareSize,
                           @RequestParam(value = "downloads" , required = false) int downloads,
                           @RequestParam(value = "appInfo" , required = false) String appInfo){
        /*for(MultipartFile file2 : file){
            String fileType = file2.getName();
            String filename = file2.getOriginalFilename();
            String ext = filename.substring(filename.lastIndexOf("."));
            long curTime = System.currentTimeMillis();
            *//* String a = File.separator+"upload"+File.separator+curTime+ext;*//*
            File file1 = new File("e:/upload");
            if(file1.exists()){
                if(file1.isDirectory()){
                    System.out.println("upload文件夹存在");
                }else {

                }
            }else{
                file1.mkdir();
            }
            try {
                file2.transferTo(Paths.get("e:","upload",String.valueOf(curTime)+ext));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
        AppInfo appInfo1 = new AppInfo();
        appInfo1.setSoftwareName(softwareName);
        appInfo1.setApkName(apkName);
        appInfo1.setSupportRom(supportRom);
        appInfo1.setInterfaceLanguage(interfaceLanguage);
        appInfo1.setFlatformId(flatformId);
        appInfo1.setCategoryLever11(categoryLeve11);
        appInfo1.setCategoryLever12(categoryLeve12);
        appInfo1.setCategoryLever13(categoryLeve13);
        appInfo1.setSoftwareSize(softwareSize);
        appInfo1.setDownloads(downloads);
        appInfo1.setAppInfo(appInfo);
        Date date = new Date(System.currentTimeMillis());
        appInfo1.setCreatedDate(date);
        int num = iAppInfoService.insertAi(appInfo1);
        if(num>0){
            return "true";
        }else {
            return "false";
        }
    }


    @ResponseBody
    @GetMapping("/updateAi")
    public String insertAi(@RequestParam(value = "softwareName" , required = false) String softwareName,
                           @RequestParam(value = "apkName" , required = false) String apkName,
                           @RequestParam(value = "supportRom" , required = false) String supportRom,
                           @RequestParam(value = "interfaceLanguage" , required = false) String interfaceLanguage,
                           @RequestParam(value = "flatformId" , required = false) int flatformId,
                           @RequestParam(value = "softwareSize" , required = false) int softwareSize,
                           @RequestParam(value = "downloads" , required = false) int downloads,
                           @RequestParam(value = "appInfo" , required = false) String appInfo,
                           @RequestParam(value = "id" , required = false) int id){
        AppInfo appInfo1 = new AppInfo();
        appInfo1.setId(id);
        appInfo1.setSoftwareName(softwareName);
        appInfo1.setApkName(apkName);
        appInfo1.setSupportRom(supportRom);
        appInfo1.setInterfaceLanguage(interfaceLanguage);
        appInfo1.setFlatformId(flatformId);
        appInfo1.setSoftwareSize(softwareSize);
        appInfo1.setDownloads(downloads);
        appInfo1.setAppInfo(appInfo);
        int num = iAppInfoService.updateAi(appInfo1);
        if(num>0){
            return "true";
        }else {
            return "false";
        }
    }
}
